﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Collections.ObjectModel;
using MTodo.Model;
using WenslyWuLib.Common;

namespace MTodo.ViewModel
{
    /// <summary>
    /// 时间组视图模型
    /// </summary>
    public class TimeGroupsViewModel:ModelBase
    {
        #region Fields
        /// <summary>
        /// New Memo Command
        /// </summary>
        public ICommand NewMemoCommand { get; set; }

        public string Test { get; set; }

        public MainPage Page { get; set; }

        public string Title { get; set; }
        #endregion

        #region Constructor
        /// <summary>
        /// 构造函数
        /// </summary>
        public TimeGroupsViewModel()
        {
            Test = "0";
            TimeGroups = new ObservableCollection<GroupItem>();
            ObservableCollection<MemoItem> memoItems = new ObservableCollection<MemoItem>();
            memoItems.Add(new MemoItem() { MemoText = "实拍歼15舰载机航母起飞降落" });

            //ObservableCollection<MemoItem> memoItems2 = new ObservableCollection<MemoItem>();
            memoItems.Add(new MemoItem() { MemoText = "美航母突发故障离开中东" });

            //ObservableCollection<MemoItem> memoItems3 = new ObservableCollection<MemoItem>();
            memoItems.Add(new MemoItem() { MemoText = "歼15战机航母首飞成功" });
            memoItems.Add(new MemoItem() { MemoText = "台雄风三增程导弹试射" });
            memoItems.Add(new MemoItem() { MemoText = "退伍老兵走好军旅征程" });
            memoItems.Add(new MemoItem() { MemoText = "国产新型武装直升机在成都军区完成列装首飞" });

            //ObservableCollection<MemoItem> memoItems2 = new ObservableCollection<MemoItem>();
            memoItems.Add(new MemoItem() { MemoText = "辽宁舰同时肩负科研试验和军事训练双重任务" });

            //ObservableCollection<MemoItem> memoItems3 = new ObservableCollection<MemoItem>();
            memoItems.Add(new MemoItem() { MemoText = "美媒：武直10将为中国军品开辟更多国际市场" });
            memoItems.Add(new MemoItem() { MemoText = "军报：歼15具备远程打击和昼夜间作战能力" });
            memoItems.Add(new MemoItem() { MemoText = "以士兵用身体拼\"失败者\"字样抗议停火决定" });

            TimeGroups.Add(new GroupItem() { GroupName = "Today", GroupId = new Guid("FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFF0"), MemoItems = memoItems });
            TimeGroups.Add(new GroupItem() { GroupName = "Tommorow", GroupId = new Guid("FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFF1"), MemoItems = memoItems });
            TimeGroups.Add(new GroupItem() { GroupName = "Last Week", GroupId = new Guid("FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFF2"), MemoItems = memoItems });
            TimeGroups.Add(new GroupItem() { GroupName = "Later", GroupId = new Guid("FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFF3"), MemoItems = memoItems});
            //
            this.NewMemoCommand = new DelegateCommand(ShowNewMemo, null);
        }

        /// <summary>
        /// Add New Items.
        /// </summary>
        /// <param name="item"></param>
        public void AddNewItem(MemoItem item)
        {
            var selectedGroup = TimeGroups.Where(o => o.GroupId == item.GroupID).FirstOrDefault();
            if (selectedGroup != null)
            {
                if (selectedGroup.MemoItems == null)
                {
                    selectedGroup.MemoItems = new ObservableCollection<MemoItem>();
                }
                selectedGroup.MemoItems.Add(item);
                this.OnPropertyChanged("TimeGroups");               
                this.OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedAction.Add);
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// 时间组
        /// </summary>
        public ObservableCollection<GroupItem> TimeGroups { get; set; }

        /// <summary>
        /// Trigger on new memo.
        /// </summary>
        /// <param name="sender"></param>
        private void ShowNewMemo(object sender)
        {
            Guid groupId = (Guid)sender;
            Page.NewWindow.GroupID = groupId;
            Page.AddViewPop.Child = Page.NewWindow;
            Page.AddViewPop.IsOpen = true;
            Page.NewWindow.ShowDiaglog();
            // e.Handled = true;
        }
        #endregion
    }
}
